const { setJson, deleteEmpty } = require("../utils/index");
const lessWork = require("../controller/lessWork");
const lessTemList = require("../models/lessTemList");

// 添加模板  createTemLists
exports.createTemLists = (req, res, next) => {
  const { body: { type, width, height, canvasBg, imgUrl, components, name } } = req;
  if(!components.length) return setJson(res, 400, "参数错误：不能保存空画布", null);
  const params = { type, width, height, canvasBg, imgUrl, components, name }
  lessTemList
    .create(params)
    .then(data => setJson(res, 200, "success", data))
    .catch(error => setJson(res, 0, "保存失败", error))
}

// 获取模板
exports.getTemLists = (req, res) => {
  const { query: { type, id } } = req;
  if(!type) return setJson(res, 400, "参数错误：缺少type", null);
  const params = deleteEmpty({ type, _id: id })
  lessTemList
    .find(params)
    .then(data => setJson(res, 200, "success", data))
    .catch(error => setJson(res, 0, "查询失败", error))
}
// 在线查看作品-获取作者作品信息，按规定作者作品登录可查看，对外在线访问时可以通过改接口访问
exports.getWorkList = (req, res) => lessWork.getWorkList(req, res)